<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">.delay()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/effects/">特效</a> &gt; <a href="/category/effects/custom-effects/">自定义</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/delay/" target="_blank">.delay()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
<article class="entry method" id="delay1"><h2 class="section-title">
<span class="name">.delay( duration [, queueName ] )</span><span class="returns">返回: <a href="/Types/#jQuery">jQuery</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>设置一个延时来推迟执行队列中后续的项。</p>
<ul class="signatures"><li class="signature" id="delay-duration-queueName">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.4/">1.4</a></span>.delay( duration [, queueName ] )</h4>
<ul>
<li>
<div><strong>duration</strong></div>
<div>类型: <a href="/Types/#Integer">Integer</a>
</div>
<div>
			一个整数，指示的毫秒数，用于设定下个队列推迟执行的时间。</div>
</li>
<li>
<div><strong>queueName</strong></div>
<div>类型: <a href="/Types/#String">String</a>
</div>
<div>一个作为队列名的字符串。默认是动画队列 <code>fx</code>。标准的效果队列。</div>
</li>
</ul>
</li></ul>
<div class="longdesc" id="entry-longdesc">
<p>在jQuery1.4中性增加的，<code>.delay()</code>方法允许我们将队列中的函数延时执行。它既可以推迟动画队列中函数的执行，也可以用于自定义队列。只有队列中连续的事件会延迟; 例如，不带参数的 <code>.show()</code> 或者 <code>.hide()</code><em>不</em>会延迟，因为他们没有使用效果队列 </p>
<p>延时时间（duration参数）是以毫秒为单位的，数值越大，动画越慢，不是越快。字符串 <code>'fast'</code> 和 <code>'slow'</code> 分别代表200和600毫秒的延时。</p>
<p>使用标准效果列队，举个例子，我们可以在 <code>&lt;div id="foo"&gt;</code> 的 <code>.slideUp()</code> 和 <code>.fadeIn()</code> 动画之间设置800毫秒的延时:</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$(<span class="string">'#foo'</span>).slideUp(<span class="number">300</span>).delay(<span class="number">800</span>).fadeIn(<span class="number">400</span>);</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>当这句语句执行的时候，这个元素会以300毫秒的卷起动画，接着暂停800毫秒，最后有400毫秒的淡入动画。</p>
<blockquote>
<p>
<strong><code>.delay()</code>是用来在jQuery动画效果和类似队列中是最好的。但是，由于其本身的限制，比如无法取消延时——<code>.delay()</code>，它不是JavaScript的原生 <a href="https://developer.mozilla.org/en/DOM/window.setTimeout">setTimeout</a>函数的替代品，这可能是更适合某些使用情况。</strong>
</p>
</blockquote>
</div>
<section class="entry-examples" id="entry-examples"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-0">
<h4><span class="desc">隐藏再显示两个div。其中绿色的div在显示之前，有800毫秒的延时。</span></h4>
<div class="syntaxhighlighter xml ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
<div class="line n20">20</div>
<div class="line n21">21</div>
<div class="line n22">22</div>
<div class="line n23">23</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="doctype">&lt;!DOCTYPE html&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">html</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">style</span>&gt;</span><span class="css"></span></code></div></div><div class="container"><div class="line"><code><span class="tag">div</span> <span class="rules">{ <span class="rule"><span class="attribute">position</span>:<span class="value"> absolute;</span></span> <span class="rule"><span class="attribute">width</span>:<span class="value"> <span class="number">60</span>px;</span></span> <span class="rule"><span class="attribute">height</span>:<span class="value"> <span class="number">60</span>px;</span></span> <span class="rule"><span class="attribute">float</span>:<span class="value"> left;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code><span class="class">.first</span> <span class="rules">{ <span class="rule"><span class="attribute">background-color</span>:<span class="value"> <span class="hexcolor">#3f3</span>;</span></span> <span class="rule"><span class="attribute">left</span>:<span class="value"> <span class="number">0</span>;</span></span><span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code><span class="class">.second</span> <span class="rules">{ <span class="rule"><span class="attribute">background-color</span>:<span class="value"> <span class="hexcolor">#33f</span>;</span></span> <span class="rule"><span class="attribute">left</span>:<span class="value"> <span class="number">80</span>px;</span></span><span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">style</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"https://code.jquery.com/jquery-latest.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">p</span>&gt;</span><span class="tag">&lt;<span class="title">button</span>&gt;</span>Run<span class="tag">&lt;/<span class="title">button</span>&gt;</span><span class="tag">&lt;/<span class="title">p</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span> <span class="attribute">class</span>=<span class="value">"first"</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span> <span class="attribute">class</span>=<span class="value">"second"</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">script</span>&gt;</span><span class="javascript"></span></code></div></div><div class="container"><div class="line"><code>    $(<span class="string">"button"</span>).click(<span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code>      $(<span class="string">"div.first"</span>).slideUp(<span class="number">300</span>).delay(<span class="number">800</span>).fadeIn(<span class="number">400</span>);</code></div></div><div class="container"><div class="line"><code>      $(<span class="string">"div.second"</span>).slideUp(<span class="number">300</span>).fadeIn(<span class="number">400</span>);</code></div></div><div class="container"><div class="line"><code>    });<span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">html</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div></section>
</div></article> </div>
</article>